package com.example.smartcity_poverty.ui.AllService.VolunteerService.ui.home

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.VolunteerService.VtBannerModel
import com.example.smartcity_poverty.logic.ServiceBean.VolunteerService.VtNewsModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.VolunteerService.adapter.VtHomeNewsAdapter
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.fragment_vt_home.*
import kotlinx.coroutines.Dispatchers

/**
 * 志愿服务主页
 */
class VtHomeFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_vt_home, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 启动协程
        runThread()
    }

    private fun runThread() = coroutine {
        // 志愿活动列表
        getNews()

        // 搜索seek
        seek()
    }

    /**
     *
     */
    val homeList = ArrayList<VtNewsModel.RowsBean>()

    private suspend fun getNews() {
        homeList.clear()
        if (getToken().isNotEmpty()) {
            api.getVtNews(getToken()).apply {
                if (this.code == 200) {
                    homeList.addAll(this.rows!!)

                    activity?.runOnUiThread {
                        vtNewsRv.apply {
                            this.isNestedScrollingEnabled = false
                            this.layoutManager = LinearLayoutManager(context)
                            this.adapter = VtHomeNewsAdapter(homeList, R.layout.re_vt_home_list)
                            this.adapter?.notifyItemChanged(homeList.size)
                        }
                    }
                } else activity?.runOnUiThread { this.msg?.show() }
            }
        } else activity?.runOnUiThread { "请先登录！".show() }
    }

    /**
     * 搜索
     */
    private fun seek() {
        vtSeek.setOnEditorActionListener { _, i, _ ->
            if (i == EditorInfo.IME_ACTION_SEARCH) {
                homeList.clear()
                coroutine(Dispatchers.Main) {
                    api.getVtNews(getToken()).apply {
                        if (this.code == 200) {
                            homeList.addAll(this.rows!!)
                            activity?.runOnUiThread {
                                activity?.runOnUiThread {
                                    vtNewsRv.apply {
                                        this.isNestedScrollingEnabled = false
                                        this.layoutManager = LinearLayoutManager(context)
                                        this.adapter =
                                            VtHomeNewsAdapter(homeList, R.layout.re_vt_home_list)
                                        this.adapter?.notifyItemChanged(homeList.size)
                                    }
                                }
                            }
                        } else activity?.runOnUiThread { this.msg?.show() }
                    }
                }
            }
            false
        }
    }

}